
//
//  HomeViewController.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/24.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "HomeNewsCell.h"
#import "SDCycleScrollView.h"
#import "UIColor+NN.h"
#import "CKPuslichTool.h"
#import "HomeViewController.h"
#import "MJRefresh.h"
#import "HandleNetProduct.h"
#import "ProductModel.h"
#import "MJExtension.h"
#import "UserModel.h"
#import "Header.h"
#import "bottomLabelCell.h"
#import "TopImageCell.h"
#import "ProductDetailController.h"
#import "ProductDetailLineController.h"
#import "NewProductDetController.h"
#import "NewProductDetLineController.h"

@interface HomeViewController ()<SDCycleScrollViewDelegate>{
    int count;
    BOOL isBottom;
    BOOL ConfirmAll;
}
@property (weak, nonatomic) IBOutlet SDCycleScrollView *myScrollView;
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property (strong,nonatomic) NSMutableArray *myArray;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topImageH;
@property (strong,nonatomic) NSMutableArray *dataArray;
@end
static BOOL isReLoad=YES;
@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"陆浦投资";//陆浦投资
    UserModel *mode=[UserModel getUser];
    if ([mode.Accountstatus intValue]==1) {
        
    
    }
//    self.topImageH.constant=Main_Screen_Width*1242/600;
    isBottom = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(gethotList)
                                                 name:@"RELOADHOMELIST"
                                               object:nil];
    
    self.dataArray=[[NSMutableArray alloc] init];
    self.mTableView.estimatedRowHeight=120;
    self.mTableView.rowHeight = UITableViewAutomaticDimension;
    _myArray=[[NSMutableArray alloc] init];
    _dataArray=[[NSMutableArray alloc] init];
    [self getImageDatas];
//    [self InitSDCycleScrollView];
    _mTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        count=1;
        [self gethotList];
    }];
    _mTableView.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        count++;
        [self gethotList];
    }];
    count=1;
    [self gethotList];
}


-(void)InitSDCycleScrollView{
//    [self.myArray addObject:@"http://s2.sinaimg.cn/middle/5ea0f97b4c75a869594b1&690"];
//    [self.myArray addObject:@"http://img5.duitang.com/uploads/item/201409/04/20140904204800_8FfKM.jpeg"];
    
    self.myScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    self.myScrollView.placeholderImage = [UIImage imageNamed:@"homeLoading"];
    self.myScrollView.infiniteLoop = YES;
    self.myScrollView.backgroundColor=[UIColor whiteColor];
    UIViewSetRadius(self.myScrollView, 0, 0.5,[UIColor groupTableViewBackgroundColor]);
    
    self.myScrollView.imageURLStringsGroup = _myArray;
//        self.myScrollView.localizationImageNamesGroup = _myArray;
    self.myScrollView.delegate=self;
    self.myScrollView.currentPageDotColor= [UIColor colorWithHexString:@"#BB3535"];
    self.myScrollView.pageDotColor=[UIColor whiteColor];
    self.myScrollView.autoScrollTimeInterval = 4.0;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)getImageDatas{
    [HandleNetProduct getHomeTopImageDataSuccess:^(NSDictionary *data) {
        NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
        NSString *url1 = data[@"TopImage1"];
        NSString *encodeUrl1 = [url1 stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
        NSString *url2 = data[@"TopImage2"];
        NSString *encodeUrl2 = [url2 stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
        [_myArray addObject:encodeUrl1];
        [_myArray addObject:encodeUrl2];
        NSLog(@"_myArray---%@",_myArray);
        if ([_myArray count] == 0) {
            [_myArray addObject:@"banner-1"];
            [_myArray addObject:@"banner-2"];
        }
        [self InitSDCycleScrollView];
//        [_mTableView reloadData];
    } Failure:^(NSString *errorInfo) {
        
    }];
}
-(void)getUserConfirmStatus{
    [HandleNetProduct getUserIDConfirmStatusForAccountPhone:[UserModel getUser].Phone Success:^(NSDictionary *data) {
        if ([data[@"InvestorConfirm"] isEqualToString:@"完成"]) {
            ConfirmAll = YES;
            [self.mTableView reloadData];
        }
    } Failure:^(NSString *errorInfo) {
        
    }];
}

-(void)gethotList{
    
    UserModel *mode=[UserModel getUser];
    if ([mode.Accountstatus intValue]==2) {
        ConfirmAll = NO;
        [self getUserConfirmStatus];
    }else{
        ConfirmAll = YES;
    }
    
    
    
    if (isBottom&&count>1) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
        return;
    }
    [HandleNetProduct getHotListforPageNo:count Success:^(NSArray *data) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
        if (count==1) {
            [self.dataArray removeAllObjects];
        }
        if (data.count>0) {
            isBottom=NO;
            [self.dataArray addObjectsFromArray:data];
            
        }else{
            isBottom=YES;
        }
        [self.mTableView reloadData];
    } Failure:^(NSString *errorInfo) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
    }];
}
#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 100;
//    
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (isBottom) {
       return _dataArray.count+1;
    }
    return _dataArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==self.dataArray.count) {
        bottomLabelCell *cell=[tableView dequeueReusableCellWithIdentifier:@"bottomLabelCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        cell.backgroundColor=[UIColor clearColor];
        cell.totalLabel.text=@"没有更多";
        return cell;
    }
    
    HomeNewsCell *cell=[tableView dequeueReusableCellWithIdentifier:@"HomeNewsCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryNone;
    cell.infoDic=_dataArray[indexPath.section];
    if (ConfirmAll) {
        cell.rate.text=_dataArray[indexPath.section][@"ExchangeRate"];
    }else{
        cell.rate.text=@"--";
    }
    [cell layoutIfNeeded];
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section==0) {
//        return;
//    }
    
    
    
//    UserModel *mode=[UserModel getUser];
    if (ConfirmAll) {
        
        NSDictionary *dic=_dataArray[indexPath.section];
        [self getdetailWith:dic[@"SubProductId"]];
    }
    
}

-(void)getdetailWith:(NSString *)subid{
     isReLoad=NO;
    NSDictionary *params=@{@"AAAMethodName":@"ProductDetail",@"SubProductId":subid};
    [HandleNetProduct getDetailForParams:params Success:^(NSDictionary *data) {
        ProductDetail *mode=[ProductDetail mj_objectWithKeyValues:data];
        if ([mode.ProductType isEqualToString:@"A"]||[mode.ProductType isEqualToString:@"B"]) {
//            ProductDetailController *vc=(ProductDetailController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Product" viewID:@"ProductDetailController"];
//            vc.productModel=mode;
//            vc.productId=subid;
//            [self.navigationController pushViewController:vc animated:YES];
            NewProductDetController *vc=(NewProductDetController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Product" viewID:@"NewProductDetController"];
            vc.productModel=mode;
            vc.productId=subid;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            
//            ProductDetailLineController *vc=(ProductDetailLineController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Product" viewID:@"ProductDetailLineController"];
//            vc.productModel=mode;
//            vc.productId=subid;
//            [self.navigationController pushViewController:vc animated:YES];
            NewProductDetLineController *vc=(NewProductDetLineController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Product" viewID:@"NewProductDetLineController"];
            vc.productModel=mode;
            vc.productId=subid;
//            vc.productTypeLevel1=productType;
            [self.navigationController pushViewController:vc animated:YES];
        }
    } Failure:^(NSString *errorInfo) {
        
    }];

    
}


@end
